package dataStruct.queue.ex3;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by thinkpad on 2016/9/15.
 */
public class Client {
    static Queue<MsgBody> msgQueue = new LinkedList<MsgBody>();

//    public static void main(String[] args) {
//        MsgTable msgTable = new MsgTable(new HashMap<String, MsgBody>());
//
//        MsgBody msgBody1 = new MsgBody("1001", State.IDLE, "1", "yc");
//        MsgBody msgBody2 = new MsgBody("1002", State.IDLE, "1", "sun");
//        MsgBody msgBody3 = new MsgBody("1003", State.IDLE, "2", "yc");
//        MsgBody msgBody4 = new MsgBody("1004", State.IDLE, "2", "sun");
//        MsgBody msgBody5 = new MsgBody("1005", State.IDLE, "3", "sun");
//        MsgBody msgBody6 = new MsgBody("1006", State.IDLE, "4", "sun");
//        MsgBody msgBody7 = new MsgBody("1007", State.IDLE, "5", "sun");
//        MsgBody msgBody8 = new MsgBody("1008", State.IDLE, "3", "yc");
//        msgTable.add(msgBody1);
//        msgTable.add(msgBody2);
//        msgTable.add(msgBody3);
//        msgTable.add(msgBody4);
//        msgTable.add(msgBody5);
//        msgTable.add(msgBody6);
//        msgTable.add(msgBody7);
//        msgTable.add(msgBody8);
//
//        msgQueue.add(msgBody1);
//        msgQueue.add(msgBody2);
//        msgQueue.add(msgBody3);
//        msgQueue.add(msgBody4);
//        msgQueue.add(msgBody5);
//        msgQueue.add(msgBody6);
//        msgQueue.add(msgBody7);
//        msgQueue.add(msgBody8);
//
//        ExecutorService service = Executors.newFixedThreadPool(5);
//        HashMap<Long, ThreadMsgInfo> threadMsgInfoHashMap = new HashMap<Long, ThreadMsgInfo>();
//
//        Producer producerYc = new Producer("yc", msgQueue, msgTable, service, null, threadMsgInfoHashMap);
//        Producer producerSun = new Producer("sun", msgQueue, msgTable, service, null, threadMsgInfoHashMap);
//
//        Thread producerThread1 = new Thread(producerYc);
//        Thread producerThread2 = new Thread(producerSun);
//        producerThread1.start();
//        producerThread2.start();
//    }

//    public static void main(String[] args) {
//        MsgTable msgTable = new MsgTable(new HashMap<String, MsgBody>());
//
//        MsgBody msgBody1 = new MsgBody("1001", State.IDLE, "1", "yc");
//        MsgBody msgBody2 = new MsgBody("1002", State.IDLE, "1", "sun");
//        MsgBody msgBody3 = new MsgBody("1003", State.IDLE, "2", "yc");
//        MsgBody msgBody4 = new MsgBody("1004", State.IDLE, "2", "sun");
//        MsgBody msgBody5 = new MsgBody("1005", State.IDLE, "3", "sun");
//        MsgBody msgBody6 = new MsgBody("1006", State.IDLE, "4", "sun");
//        MsgBody msgBody7 = new MsgBody("1007", State.IDLE, "5", "sun");
//        MsgBody msgBody8 = new MsgBody("1008", State.IDLE, "3", "yc");
//
//        ExecutorService service = Executors.newFixedThreadPool(5);
//        HashMap<Long, ThreadMsgInfo> threadMsgInfoHashMap = new HashMap<Long, ThreadMsgInfo>();
//        service.submit(new Scheduler(msgQueue, msgTable, threadMsgInfoHashMap));
//
//        Producer producerYc1 = new Producer(msgBody1.getName(), msgQueue, msgTable, service, msgBody1);
//        Producer producerSun1 = new Producer(msgBody2.getName(), msgQueue, msgTable, service, msgBody2);
//        Producer producerYc2 = new Producer(msgBody1.getName(), msgQueue, msgTable, service, msgBody3);
//        Producer producerSun2 = new Producer(msgBody2.getName(), msgQueue, msgTable, service, msgBody4);
//        Producer producerSun3 = new Producer(msgBody2.getName(), msgQueue, msgTable, service, msgBody5);
//        Producer producerSun4 = new Producer(msgBody2.getName(), msgQueue, msgTable, service, msgBody6);
//        Producer producerSun5 = new Producer(msgBody2.getName(), msgQueue, msgTable, service, msgBody7);
//        Producer producerYc3 = new Producer(msgBody1.getName(), msgQueue, msgTable, service, msgBody8);
//
//        Thread producerThread1 = new Thread(producerYc1);
//        Thread producerThread2 = new Thread(producerSun1);
//        Thread producerThread3 = new Thread(producerYc2);
//        Thread producerThread4 = new Thread(producerSun2);
//        Thread producerThread5 = new Thread(producerSun3);
//        Thread producerThread6 = new Thread(producerSun4);
//        Thread producerThread7 = new Thread(producerSun5);
//        Thread producerThread8 = new Thread(producerYc3);
//
//        producerThread1.start();
//        producerThread2.start();
//        producerThread3.start();
//        producerThread4.start();
//        producerThread5.start();
//        producerThread6.start();
//        producerThread7.start();
//        producerThread8.start();
//
//    }

    public static void main(String[] args) {
        MsgTable msgTable = new MsgTable(new HashMap<String, MsgBody>());

        MsgBody msgBody1 = new MsgBody("1001", State.IDLE, "1", "yc");
        MsgBody msgBody2 = new MsgBody("1002", State.IDLE, "1", "sun");
        MsgBody msgBody3 = new MsgBody("1003", State.IDLE, "2", "yc");
        MsgBody msgBody4 = new MsgBody("1004", State.IDLE, "2", "sun");
        MsgBody msgBody5 = new MsgBody("1005", State.IDLE, "3", "sun");
        MsgBody msgBody6 = new MsgBody("1006", State.IDLE, "4", "sun");
        MsgBody msgBody7 = new MsgBody("1007", State.IDLE, "5", "sun");
        MsgBody msgBody8 = new MsgBody("1008", State.IDLE, "3", "yc");

        HashMap<Long, ThreadMsgInfo> threadMsgInfoHashMap = new HashMap<Long, ThreadMsgInfo>();

        Producer producerYc1 = new Producer(msgBody1.getName(), msgQueue, msgTable, null, msgBody1,threadMsgInfoHashMap);
        Producer producerSun1 = new Producer(msgBody2.getName(), msgQueue, msgTable, null, msgBody2,threadMsgInfoHashMap);
        Producer producerYc2 = new Producer(msgBody1.getName(), msgQueue, msgTable, null, msgBody3,threadMsgInfoHashMap);
        Producer producerSun2 = new Producer(msgBody2.getName(), msgQueue, msgTable, null, msgBody4,threadMsgInfoHashMap);
        Producer producerSun3 = new Producer(msgBody2.getName(), msgQueue, msgTable, null, msgBody5,threadMsgInfoHashMap);
        Producer producerSun4 = new Producer(msgBody2.getName(), msgQueue, msgTable, null, msgBody6,threadMsgInfoHashMap);
        Producer producerSun5 = new Producer(msgBody2.getName(), msgQueue, msgTable, null, msgBody7,threadMsgInfoHashMap);
        Producer producerYc3 = new Producer(msgBody1.getName(), msgQueue, msgTable, null, msgBody8,threadMsgInfoHashMap);

        Thread producerThread1 = new Thread(producerYc1);
        Thread producerThread2 = new Thread(producerSun1);
        Thread producerThread3 = new Thread(producerYc2);
        Thread producerThread4 = new Thread(producerSun2);
        Thread producerThread5 = new Thread(producerSun3);
        Thread producerThread6 = new Thread(producerSun4);
        Thread producerThread7 = new Thread(producerSun5);
        Thread producerThread8 = new Thread(producerYc3);

        producerThread1.start();
        producerThread2.start();
        producerThread3.start();
        producerThread4.start();
        producerThread5.start();
        producerThread6.start();
        producerThread7.start();
        producerThread8.start();

        ExecutorService service = Executors.newFixedThreadPool(5);
        for (int i = 0; i < 5; i++) {
            service.submit(new Scheduler(msgQueue, msgTable, threadMsgInfoHashMap));
        }
    }

}
